import Vue from 'vue'
import VueRouter from 'vue-router'
import store from '@/store/index'
Vue.use(VueRouter)

import Layout from '@/views/Layout'
import Login from '@/views/Login.vue'
import Register from '@/views/Register.vue'
import product from '@/views/product.vue'
import test from '@/views/test.vue'
const routes = [
  {
    path:'/',
    name:'Layout',
    component:Layout,
  },
  {
      path:'/login',
      component:Login
  },
  {
      path:'/register',
      component:Register 
  },
  {
    path:'/cart',
    component:test,
    beforeEnter: (to,from, next) => {
      if(!store.state.loginStatus){
        alert('请先登录')
        next('/login')
      }else{
        next()
      }
    }

  },
  {
    path:'/product/:id',
    component:product
  },
]

const router = new VueRouter({
  routes
})
const originalPush = VueRouter.prototype.push
VueRouter.prototype.push = function push(location, onResolve, onReject) {
    if (onResolve || onReject) return originalPush.call(this, location, onResolve, onReject)
    return originalPush.call(this, location).catch(err => err)
}
export default router
